home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / MP3 Digita256668312001.psc / modGeneral.bas < prev    next >
Encoding:
BASIC Source File  |  2001-08-31  |  10.8 KB  |  401 lines

  1. Attribute VB_Name = "modGeneral"
  2. Option Explicit
  3.  
  4. ' /*
  5. ' ** Allocates the specified number of bytes from the heap.
  6. ' */
  7. Public Declare Function GlobalAlloc _
  8.     Lib "kernel32" ( _
  9.         ByVal wFlags As Long, _
  10.         ByVal dwBytes As Long) As Long
  11.  
  12. ' /*
  13. ' ** Locks a global memory object and returns a pointer to
  14. ' ** the first byte of the bject's memory block.
  15. ' ** The memory block associated with a locked object cannot
  16. ' ** be moved or discarded.
  17. '*/
  18. Public Declare Function GlobalLock _
  19.     Lib "kernel32" ( _
  20.         ByVal hmem As Long) As Long
  21.  
  22. ' /*
  23. ' ** Frees the specificed global memory object and
  24. ' ** invalidates its handle
  25. ' */
  26. Public Declare Function GlobalFree _
  27.     Lib "kernel32" ( _
  28.         ByVal hmem As Long) As Long
  29.  
  30. Public Declare Sub CopyPtrFromStruct _
  31.     Lib "kernel32" _
  32.     Alias "RtlMoveMemory" ( _
  33.         ByVal ptr As Long, _
  34.         struct As Any, _
  35.         ByVal cb As Long)
  36.         
  37. Public Declare Sub memcpy _
  38.     Lib "kernel32" _
  39.     Alias "RtlMoveMemory" ( _
  40.         ptr1 As Any, _
  41.         Ptr2 As Any, _
  42.         ByVal cb As Long)
  43.         
  44. Public Declare Sub CopyMemory _
  45.     Lib "kernel32" _
  46.     Alias "RtlMoveMemory" ( _
  47.         ByVal ptr1 As Long, _
  48.         ByVal Ptr2 As Long, _
  49.         ByVal cb As Long)
  50.  
  51. Public Declare Sub CopyStructFromPtr _
  52.     Lib "kernel32" _
  53.     Alias "RtlMoveMemory" ( _
  54.         struct As Any, _
  55.         ByVal ptr As Long, _
  56.         ByVal cb As Long)
  57.  
  58. Public Type SHITEMID    'Browse Dialog
  59.    cb             As Long
  60.    abID           As Byte
  61. End Type
  62.  
  63. Public Type ITEMIDLIST  'Browse Dialog
  64.    mkid           As SHITEMID
  65. End Type
  66.  
  67. Public Type BROWSEINFO  'Browse Dialog
  68.    hOwner         As Long
  69.    pidlRoot       As Long
  70.    pszDisplayName As String
  71.    lpszTitle      As String
  72.    ulFlags        As Long
  73.    lpfn           As Long
  74.    lParam         As Long
  75.    iImage         As Long
  76. End Type
  77.  
  78. Public Const BIF_RETURNONLYFSDIRS = &H1 'Browse Dialog
  79. Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
  80. Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
  81.  
  82. Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ptr() As Any) As Long
  83.  
  84.  
  85.  
  86. Public Cancelled As Boolean
  87.  
  88. ' This removes Nulls from arrays and returns string
  89. Public Function StripNullsArray(STR) As String
  90.   Dim i As Long
  91.   For i = LBound(STR) To UBound(STR)
  92.     If STR(i) <> 0 Then StripNullsArray = StripNullsArray & Chr(STR(i))
  93.   Next i
  94. End Function
  95.  
  96. ' This removes Nulls from string
  97. Public Function StripNulls(STR As String) As String
  98.   StripNulls = Left(STR, InStr(STR, Chr(0)) - 1)
  99. End Function
  100.  
  101. Public Function ChangeExt(Filename As String, NewExt As String)
  102.   ChangeExt = Left(Filename, InStrRev(Filename, ".")) & NewExt
  103. End Function
  104.  
  105. ' This adds slash to path if it is not there...
  106. Public Function AddSlash(FullPath As String) As String
  107.   AddSlash = IIf(Right(FullPath, 1) = "\", FullPath, FullPath & "\")
  108. End Function
  109.  
  110. 'Opens Browse dialog
  111. Public Function BrowseForFolder(Optional Title As String) As String
  112.    Dim bi As BROWSEINFO
  113.    Dim pidl As Long
  114.    Dim nRet As Long
  115.    Dim szPath As String
  116.    
  117.    szPath = Space$(512)
  118.    
  119.    bi.hOwner = 0&
  120.    bi.pidlRoot = 0&
  121.    
  122.    bi.lpszTitle = IIf(Title = "", "Directory", Title)
  123.    bi.ulFlags = BIF_RETURNONLYFSDIRS
  124.    
  125.    'Display the dialog and get the selected path
  126.    pidl& = SHBrowseForFolder(bi)
  127.    SHGetPathFromIDList ByVal pidl&, ByVal szPath
  128.    
  129.    'Return value
  130.    BrowseForFolder = Trim$(szPath)
  131. End Function
  132.  
  133. ' Extract track to MP3 file
  134. Public Sub RipMP3(outMP3Name As String, StartAddr As Long, EndAddr As Long, beConfig As PBE_CONFIG)
  135.   On Error GoTo ErrHandler
  136.   ChDrive App.Path
  137.   ChDir App.Path
  138.   
  139.   ' Check that file exists...
  140.   Cancelled = False
  141.   
  142.   'Open progress window
  143.   frmProgress.Show , frmMain
  144.   
  145.   Dim error As Long
  146.   Dim dwSamples As Long, dwMP3Buffer As Long, hbeStream As Long
  147.   
  148.   ' Init MP3 Stream
  149.   error = beInitStream(VarPtr(beConfig), VarPtr(dwSamples), VarPtr(dwMP3Buffer), VarPtr(hbeStream))
  150.     
  151.   '// Check result
  152.   If error <> BE_ERR_SUCCESSFUL Then
  153.     Err.Raise error, "Lame", GetErrorString(error)
  154.   End If
  155.   
  156.   
  157.   ' Open MP3 file...
  158.   Dim WriteFile As clsFileIo
  159.   Set WriteFile = New clsFileIo
  160.   WriteFile.OpenFile outMP3Name
  161.   
  162.   
  163.   Dim NumFrames   As Long
  164.   Dim Dummy       As PTRACKBUF
  165.   Dim BufferPtr1  As Long
  166.   Dim BufferPtr2  As Long
  167.   Dim LLen        As Long
  168.   Dim Retries     As Long
  169.   Dim Status      As Long
  170.   Dim NumWritten  As Long
  171.   Dim toRead      As Long, toWrite As Long
  172.   Dim Done        As Long
  173.   Dim length      As Long
  174.   
  175.   NumFrames = SECTORSPERREAD
  176.     
  177.   'Initialize Audio Extraction buffer
  178.   BufferPtr1 = GlobalAlloc(&H40, NumFrames * 2352 + Len(Dummy))
  179.   BufferPtr2 = GlobalLock(BufferPtr1)
  180.   
  181.   ' Dummy is used to inform AKRip what to extract from CD
  182.   Dummy.startFrame = 0
  183.   Dummy.NumFrames = 0
  184.   Dummy.maxLen = NumFrames * 2352
  185.   Dummy.len = 0
  186.   Dummy.Status = 0
  187.   Dummy.startOffset = 0
  188.   
  189.   'We copy Dummy into buffer...
  190.   CopyMemory ByVal BufferPtr2, ByVal VarPtr(Dummy), Len(Dummy)
  191.   
  192.   Dim temp As Long
  193.   temp = EndAddr - StartAddr
  194.   LLen = EndAddr - StartAddr
  195.  
  196.   ' Allocate memory for MP3 buffer...
  197.   Dim MP3Ptr1 As Long
  198.   Dim MP3Ptr2 As Long
  199.   
  200.   MP3Ptr1 = GlobalAlloc(&H40, dwMP3Buffer)
  201.   MP3Ptr2 = GlobalLock(MP3Ptr1)
  202.   
  203.   Dim NoOfBytes2Encode As Long
  204.   
  205.   Dim i As Long
  206.   
  207.   ' Lets start MP3 Extraction...
  208.   
  209.   Do While LLen
  210.     ' Calculate how much we wanna rip from CD
  211.     If LLen < NumFrames Then NumFrames = LLen
  212.       
  213.     Retries = RetriesCount
  214.     Status = 0
  215.     
  216.     ' Try to read cd...
  217.     Do While Retries > 0 And Status <> 1
  218.       Dummy.NumFrames = NumFrames
  219.       Dummy.startOffset = 0
  220.       Dummy.len = 0
  221.       Dummy.startFrame = StartAddr
  222.       
  223.       'Write info to buffer so that akrip knows what to read... :)
  224.       CopyMemory ByVal BufferPtr2, ByVal VarPtr(Dummy), Len(Dummy)
  225.       
  226.       Status = ReadCDAudioLBA(CDHandle, BufferPtr2)
  227.     Loop
  228.     
  229.     If Status <> 1 Then
  230.       ' This is bad.... and there is nothing we can do...
  231.       MsgBox GetAKRipError
  232.       Exit Do
  233.     End If
  234.     
  235.     ' Encode every frame we just extracted from CD
  236.     For i = 0 To NumFrames - 1
  237.       NoOfBytes2Encode = 2352 / 2 'One frame is 2352 bytes
  238.                                   'Note: it is splitted because
  239.                                   'LAME uses "Short" samples for encoding
  240.       ' Encode buffer
  241.       ' Note: Don't encode info... Memory position is pointer + lenght of Dummy
  242.       error = beEncodeChunk(hbeStream, NoOfBytes2Encode, BufferPtr2 + Len(Dummy) + i * 2352, MP3Ptr2, VarPtr(toWrite))
  243.       
  244.       ' Write buffer to HardDrive buffer
  245.       If toWrite > 0 Then WriteFile.WriteBytes MP3Ptr2, toWrite
  246.     Next i
  247.     
  248.     ' Write HardDrive buffers to disk
  249.     Call WriteFile.FlushBuffers
  250.     
  251.     
  252.     ' We have written this much bytes and blahblahblahblah.... :)
  253.     NumWritten = NumWritten + NumFrames * 2352
  254.     StartAddr = StartAddr + NumFrames
  255.     LLen = LLen - NumFrames
  256.     
  257.     If Cancelled Then Exit Do
  258.     'Inform user where we go...
  259.     frmProgress.ChangeProgress "Extracting track " & outMP3Name, CSng((temp - LLen)), CSng(temp)
  260.     DoEvents
  261.   Loop
  262.     
  263.     
  264.   
  265.   ' Deinitialize stream and write last bytes to MP3
  266.   error = beDeinitStream(hbeStream, MP3Ptr2, VarPtr(toWrite))
  267.  
  268.   '//if close out was unsuccessful manually close stream
  269.   If toWrite > 0 Then
  270.     WriteFile.WriteBytes MP3Ptr2, toWrite
  271.     WriteFile.FlushBuffers
  272.   End If
  273.   
  274.   
  275.   ' Clear buffers....
  276.   GlobalFree MP3Ptr2
  277.   GlobalFree BufferPtr2
  278.   
  279.   ' Close files
  280.   Call WriteFile.CloseFile
  281.   Set WriteFile = Nothing
  282.   
  283.   ' Close stream
  284.   Call beCloseStream(hbeStream)
  285.   
  286.   
  287.   ' WriteVBRHeader (if we use variable bitrate...)
  288.   'Call beWriteVBRHeader(ChangeExt(Text1, "mp3"))
  289.   Unload frmProgress
  290.   
  291.   Exit Sub
  292.   
  293. ErrHandler:
  294.   ' Damn.. Something went wrong and this one should tell what...
  295.   
  296.   MsgBox Err.Description, vbCritical, "Critical error..."
  297.   If BufferPtr2 Then GlobalFree BufferPtr2
  298.   If MP3Ptr2 Then GlobalFree MP3Ptr2
  299.   WriteFile.FlushBuffers
  300.   WriteFile.CloseFile
  301.   Unload frmProgress
  302.   Err.Clear
  303. End Sub
  304.  
  305. ' Extract track to WAV file
  306. Public Function RipWAV(Filename As String, addrStart As Long, addrEnd As Long)
  307.   Dim StartAddr   As Long
  308.   Dim EndAddr     As Long
  309.   Dim NumFrames   As Long
  310.   Dim Dummy       As PTRACKBUF
  311.   Dim BufferPtr1  As Long
  312.   Dim BufferPtr2  As Long
  313.   Dim LLen        As Long
  314.   Dim Retries     As Long
  315.   Dim Status      As Long
  316.   Dim NumWritten  As Long
  317.   Dim OpenFile As clsFileIo
  318.   
  319.   Cancelled = False
  320.   frmProgress.Show , frmMain
  321.  
  322.   NumFrames = SECTORSPERREAD
  323.    
  324.   ' Convert Addresses
  325.   StartAddr = addrStart
  326.   EndAddr = addrEnd
  327.   
  328.   'Initialize buffer
  329.   BufferPtr1 = GlobalAlloc(&H40, NumFrames * 2352 + Len(Dummy))
  330.   BufferPtr2 = GlobalLock(BufferPtr1)
  331.   
  332.   Dummy.startFrame = 0
  333.   Dummy.NumFrames = 0
  334.   Dummy.maxLen = NumFrames * 2352
  335.   Dummy.len = 0
  336.   Dummy.Status = 0
  337.   Dummy.startOffset = 0
  338.   
  339.   CopyMemory ByVal BufferPtr2, ByVal VarPtr(Dummy), Len(Dummy)
  340.   
  341.   Dim temp As Long
  342.   temp = EndAddr - StartAddr
  343.   LLen = EndAddr - StartAddr
  344.   
  345.   ' Open files
  346.   Set OpenFile = New clsFileIo
  347.   
  348.   OpenFile.OpenFile Filename
  349.   OpenFile.writeWavHeader LLen * 2352
  350.   
  351.   Dim TempCount As Byte
  352.   
  353.   ' Lets start rippin...
  354.   Do While LLen
  355.     ' Calculate how much we wanna rip from CD
  356.     If LLen < NumFrames Then NumFrames = LLen
  357.       
  358.     Retries = RetriesCount
  359.     Status = 0
  360.     
  361.     ' Try to read cd...
  362.     Do While Retries > 0 And Status <> 1
  363.       Dummy.NumFrames = NumFrames
  364.       Dummy.startOffset = 0
  365.       Dummy.len = 0
  366.       Dummy.startFrame = StartAddr
  367.       
  368.       'Write info to buffer so that akrip knows what to read... :)
  369.       CopyMemory ByVal BufferPtr2, ByVal VarPtr(Dummy), Len(Dummy)
  370.       
  371.       Status = ReadCDAudioLBA(CDHandle, BufferPtr2)
  372.     Loop
  373.     
  374.     If Status = 1 Then
  375.       ' Write buffer to disk
  376.       ' Note: Don't write info to disk... Memory position is pointer + lenght of Dummy
  377.       OpenFile.WriteBytes BufferPtr2 + Len(Dummy), NumFrames * 2352
  378.     Else
  379.       ' Doh.... This is bad.... and there is nothing we can do...
  380.       MsgBox GetAKRipError
  381.       Exit Do
  382.     End If
  383.     
  384.     ' We have written this much bytes and blahblahblahblah.... :)
  385.     NumWritten = NumWritten + NumFrames * 2352
  386.     StartAddr = StartAddr + NumFrames
  387.     LLen = LLen - NumFrames
  388.     
  389.     'Inform user where we go...
  390.     frmProgress.ChangeProgress "Extracting track " & Filename, CSng(temp - LLen), CSng(temp)
  391.     DoEvents
  392.   Loop
  393.   
  394.   ' Delete buffer and close files
  395.   GlobalFree BufferPtr2
  396.   OpenFile.CloseFile
  397.   Set OpenFile = Nothing
  398.   Unload frmProgress
  399. End Function
  400.  
  401.